import {app, BrowserWindow, globalShortcut, Menu} from 'electron'
import {join} from 'path'

// 屏蔽安全警告
// electron Security Warning (Insecure Content-Security-Policy)
process.env['ELECTRON_DISABLE_SECURITY_WARNINGS'] = 'true'

let win
const createWindow = () => {
    win = new BrowserWindow({
        // 窗口图标
        // icon: join(__dirname, "resource/shortcut.ico"),
        width: 1400,
        height: 800,
        webPreferences: {
            nodeIntegration: true,
            contextIsolation: false,
            webSecurity: false,
            // preload: path.join(__dirname, 'preload.js')
        },
    })
    if (process.env.VITE_DEV_SERVER_URL) {
        win.loadURL(process.env.VITE_DEV_SERVER_URL)
        win.webContents.openDevTools()
    } else {
        win.loadFile(join(__dirname, 'dist/index.html'))
    }
    createMenu()
}

// 设置菜单栏
function createMenu() {
    // darwin表示macOS，针对macOS的设置
    if (process.platform !== 'darwin') {
        // windows及linux系统
        Menu.setApplicationMenu(null)
    }
}


app.on('activate', () => {
    if (BrowserWindow.getAllWindows().length === 0) {
        createWindow()
    }
})
app.whenReady().then(() => {
    globalShortcut.register('ctrl+f12', function () {
        win.webContents.openDevTools()
    })
    createWindow()
})

app.on('window-all-closed', () => {
    if (process.platform !== 'darwin') app.quit()
})
